import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import "@/plugins/vant.js";
import "@/plugins/global.js";
import Meta from "vue-meta";
import { network } from "@/config/gateway";
import "@/assets/js/media_750";
import axios from "axios";
import "@/assets/css/base.css";
import "@/assets/css/css.css";
Vue.use(Meta);
Vue.config.productionTip = true;
new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount("#app");

axios.defaults.baseURL = network;
Vue.prototype.$axios = axios;
axios.interceptors.request.use(
  (config) => {
    if (store.state.token) {
      config.headers["Authori-zation"] = "Bearer " + store.state.token;
    }
    return config;
  },
  // 处理处理
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截
axios.interceptors.response.use(
  (response) => {
    // Indicator.close();
    if (response.status == 200) {
      const data = response.data;
      var oDate1 = new Date(localStorage.getItem('expires_time')).getTime();//token失效时间
      var oDate2 =new Date().getTime()//当前时间
      if (oDate1<=oDate2) {
        //调转到login界面
        console.log('登录过期 需求重新登录')
        //登录过期 需求重新登录 情况vuex的token和localstorge的token
        store.commit("settoken", "");
        localStorage.removeItem("token");
        router.replace({ path: "/login" });
      }
      return data;
    }
    return response.data;
  },
  // 处理处理
  (error) => {
    return Promise.reject(error);
  }
);

